package jm.mq.prods;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import jm.mq.utils.RabbitMQUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @Description 优先级队列消费者
 * @date 2022/3/14 15:16
 */
public class Producer {

    private static final String QUEUE_NAME = "priority_queue";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMQUtils.getChannel();

        Map<String ,Object> argument = new HashMap<>();
        argument.put("x-max-priority",10);
        channel.queueDeclare(QUEUE_NAME,false,false,false,argument);

        int priorityVal = 2;

        for (int j = 0; j < 10; j++) {
            String msg = "优先队列发送消息，顺序：" + j ;
            if (j % 2 == 0){
                msg += "优先级：" + priorityVal;
                AMQP.BasicProperties properties =
                        new AMQP.BasicProperties().builder().priority(priorityVal).build();
                channel.basicPublish("",QUEUE_NAME,properties, msg.getBytes(StandardCharsets.UTF_8));
                priorityVal ++;
            }else{
                channel.basicPublish("",QUEUE_NAME,null, msg.getBytes(StandardCharsets.UTF_8));
            }
            System.out.println("发送成功：" + msg);
        }
        System.out.println("----------==========发送完毕==========----------");
    }
}
